package com.xue.util;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @program: springboot-outbreak
 * @description: 请求的工具
 * @create: 2020-05-28 08:53
 **/
public class HttpClientUtil {

    public static String doGet(String url){

        //闭合的httpclient对象
        CloseableHttpClient httpClient = null;
        //闭合的响应对象
        CloseableHttpResponse response = null;
        String result = null;
        try {
            // // 使用默认创建方式
            httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(url);
            //  // 设置请求头的方式
            httpGet.addHeader("Accept","application/json");
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(35000)
                    .setConnectionRequestTimeout(35000)
                    .setSocketTimeout(60000).build();
            httpGet.setConfig(requestConfig);
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        String url = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_other";
        String doGet = doGet(url);
        System.out.println(doGet);
    }
}
